package com.aoxon.cargo.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Cloth;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.bean.Picture;
import com.aoxon.cargo.cache.SupCache;
import com.aoxon.cargo.component.MyAlertDialog;
import com.aoxon.cargo.component.MyCallBack;
import com.aoxon.cargo.component.MyPriceItem;
import com.aoxon.cargo.component.MyProgressDialog;
import com.aoxon.cargo.component.MySquarePicture;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.DeleteGoodsService;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.EnvironmentUtil;
import com.aoxon.cargo.util.ToastUtil;
import com.google.gson.reflect.TypeToken;

/**
 * 供应商商品详情
 * 
 * @author ZZJ
 * 
 */
public class SupGoodsDetailsActivity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {

	private ImageView ivReturnButton;

	private TextView tvGoodsName;
	private TextView tvGoodsNumber;
	private TextView tvGoodsClassifyOne;
	private TextView tvGoodsClassifyTwo;
	private TextView tvGoodsDescribe;
	private TextView tvRevampButton;
	private TextView tvDeleteButton;

	private LinearLayout llGoodsPicture;
	private LinearLayout llGoodsPrice;

	private MyPriceItem myPriceItem;
	private MyAlertDialog myAlertDialog;
	private MySquarePicture mySquarePicture;
	private MyProgressDialog myProgressDialog;

	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);

	private DeleteGoodsService deleteGoodsService = (DeleteGoodsService) MyServiceFactory
			.getInstance(MyServiceFactory.DELETE_GOODS);
	private GsonBean gsonBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setup();

		beginShow();

	}

	/**
	 * 自定义方法
	 * ******************************************************************************************************************************************/

	private void setup() {
		setContentView(R.layout.sup_goods_details);

		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);

		tvGoodsName = (TextView) findViewById(R.id.tvGoodsDetailsName);
		tvGoodsNumber = (TextView) findViewById(R.id.tvGoodsDetailsNumber);
		tvGoodsClassifyOne = (TextView) findViewById(R.id.tvGoodsDetailsClassifyOne);
		tvGoodsClassifyTwo = (TextView) findViewById(R.id.tvGoodsDetailsClassifyTwo);
		tvGoodsDescribe = (TextView) findViewById(R.id.tvGoodsDetailsDescribe);
		tvRevampButton = (TextView) findViewById(R.id.tvSupGoodsDetailsRevampButton);
		tvDeleteButton = (TextView) findViewById(R.id.tvSupGoodsDetailsDeleteButton);

		llGoodsPicture = (LinearLayout) findViewById(R.id.llGoodsDetailsPicture);
		llGoodsPrice = (LinearLayout) findViewById(R.id.llGoodsDetailsPrice);

		myAlertDialog = new MyAlertDialog(this);
		myProgressDialog = new MyProgressDialog(this);
		mySquarePicture = new MySquarePicture(this, llGoodsPicture, false,
				false);
		myPriceItem = new MyPriceItem(getBaseContext(), llGoodsPrice);

		normalDataLoader.setOnloadDataListener(this);

		ivReturnButton.setOnClickListener(myClickListener);
		tvRevampButton.setOnClickListener(myClickListener);
		tvDeleteButton.setOnClickListener(myClickListener);
	}

	/**
	 * 初始化显示
	 */
	private void beginShow() {

		try {
			final Cloth cloth = SupCache.cloth;
			SupCache.pictureList = cloth.getPictures();
			Log.i("GoodsDetails", cloth.toString());
			tvGoodsName.setText(cloth.getClothName());
			tvGoodsNumber.setText(cloth.getClothItemNumber());
			if (cloth.getPrices() != null) {
				myPriceItem.show(cloth.getPrices());
			}
			tvGoodsClassifyOne.setText(cloth.getClothGeneralType());
			tvGoodsClassifyTwo.setText(cloth.getClothDetailsType());
			tvGoodsDescribe.setText(cloth.getClothDescribe());

			if (cloth.getPictures() != null) {
				for (int i = 0; i < cloth.getPictures().size(); i++) {
					if (EnvironmentUtil.checkNetState(getBaseContext())) {
						mySquarePicture.show(cloth.getPictures().get(i));
					} else {
						ToastUtil.show(getBaseContext(), "无网络连接");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ToastUtil.show(getBaseContext(), "物品信息出错,请刷新数据重试！");
			Intent intent = new Intent(getBaseContext(),
					SupGoodsManagerActivity.class);
			startActivity(intent);
			this.finish();
		}

	}

	/**
	 * 
	 */
	private void deleteResult() {
		try {
			if (CheckStateUtil.check(getBaseContext(), gsonBean, "删除")) {
				SupCache.cloth = null;
				Intent intent = new Intent(getBaseContext(),
						SupGoodsManagerActivity.class);
				startActivity(intent);
				this.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
			ToastUtil.show(getBaseContext(), "网络连接异常");
		}
	}

	/**
	 * 确定删除的回调
	 * 
	 */
	class sureDelete implements MyCallBack {
		@Override
		public void callback(String choose) {
			normalDataLoader.load();
		}

	}

	/**
	 * 系统按键的设置
	 * *****************************************************************************************************************************************/

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(getBaseContext(),
					SupGoodsManagerActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}

	/**
	 * 设置各控件的点击监听
	 */
	OnClickListener myClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {

			case R.id.ivPublicTitleReturn:
				intent = new Intent(getBaseContext(),
						SupGoodsManagerActivity.class);
				startActivity(intent);
				SupGoodsDetailsActivity.this.finish();
				break;

			case R.id.tvSupGoodsDetailsRevampButton:
				intent = new Intent(getBaseContext(),
						SupInputGoodsInfoActivity.class);
				SupInputGoodsInfoActivity.TO_ADD_GOODS = false;
				SupCache.isChangePicture = false;
				SupCache.from = 2;
				String str = DataUtil.gson.toJson(SupCache.cloth.getPictures());
				SupCache.pictureList = DataUtil.gson.fromJson(str,
						new TypeToken<List<Picture>>() {
						}.getType());
				startActivity(intent);
				SupGoodsDetailsActivity.this.finish();
				break;

			case R.id.tvSupGoodsDetailsDeleteButton:
				myAlertDialog.show("确定删除？", new sureDelete());
				break;

			default:
				break;
			}

		}
	};

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		myProgressDialog.show("提示", "删除中");
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		gsonBean = deleteGoodsService.execute(SupCache.cloth.getClothId());
		msg.what = MyStateUtil.GOODS_RESULT_CODE_DELETE;
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		myProgressDialog.cancle();
		switch (state) {
		case MyStateUtil.GOODS_RESULT_CODE_DELETE:
			deleteResult();
			break;
		case MyStateUtil.NOT_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "无网络连接");
			break;
		default:
			break;
		}
	}

}
